import Cookies from 'js-cookie'

const tokenKey = 'user-info-token-base';
const userInfo = 'user-info-customer-base';

// token
export function getToken() {
  return Cookies.get(tokenKey)
}

export function setToken(token) {
  return Cookies.set(tokenKey, token)
}

export function removeToken() {
  return Cookies.remove(tokenKey)
}

// userInfo
export function setUserInfo(value) {
  write(userInfo, value);
}

export function getUserInfo() {
  return read(userInfo);
}

export function removeUserInfo() {
  return remove(userInfo);
}

// localStorage
export function write(key, value) {
  if (value) {
    value = JSON.stringify(value);
  }
  localStorage.setItem(key, value);
}

export function read(key) {
  let value = localStorage.getItem(key);
  if (value && value != "undefined" && value != "null") {
    return JSON.parse(value);
  }
  return null
}

export function remove(key) {
  localStorage.removeItem(key);
}

export function clear() {
  localStorage.clear();
}

// session storage
export function writeSession(key, value) {
  if (value) {
    value = JSON.stringify(value);
  }
  sessionStorage.setItem(key, value);
}

export function readSession(key) {
  let value = sessionStorage.getItem(key);
  if (value && value != "undefined" && value != "null") {
    return JSON.parse(value);
  }
  return null
}

export function removeSession(key) {
  sessionStorage.removeItem(key);
}

export function clearSession() {
  sessionStorage.clear();
}

// 初始密码
export function setPassword(password) {
  return Cookies.set('web-password', password)
}

export function getPassword() {
  return Cookies.get('web-password')
}

// 头部导航id
export function setApplicationId(applicationId) {
  return Cookies.set('applicationId', applicationId)
}

export function getApplicationId() {
  return Cookies.get('applicationId')
}

// 头部导航选中样式
export function setApplicationIndex(index) {
  return Cookies.set('activeIndex', index)
}

export function getApplicationIndex() {
  return Cookies.get('activeIndex')
}

// 头部导航数组
export function setTopNavList(topNavList) {
  return Cookies.set('topNavList', topNavList)
}

export function getTopNavList() {
  return Cookies.get('topNavList')
}
